package com.szd.qiongyou20.community.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.szd.qiongyou20.R;
import com.szd.qiongyou20.community.adapter.Recon_GridView_Adapter;
import com.szd.qiongyou20.community.adapter.Recon_RecycylerViewH_Adapter;
import com.szd.qiongyou20.community.adapter.Recon_RecycylerViewV_Adapter;
import com.szd.qiongyou20.community.bean.RecommendBean;
import com.szd.qiongyou20.community.pre.ReconPre;
import com.szd.qiongyou20.community.view.ReconDataToView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by SZD on 2016/12/28.
 */

public class Fragment_Recommend extends Fragment implements ReconDataToView {


    @Bind(R.id.recyclerView_recommend_h)
    RecyclerView mRecyclerViewRecommendH;
    @Bind(R.id.recyclerView_recommend_v)
    RecyclerView mRecyclerViewRecommendV;
    @Bind(R.id.myGridView_recommend)
    GridView mMyGridViewRecommend;

    private Context mContext;

    private ReconPre mReconPre;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getActivity();
        mReconPre = new ReconPre(this);
        mReconPre.loadDataToView();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, null);
        mMyGridViewRecommend = (GridView) view.findViewById(R.id.myGridView_recommend);
        mRecyclerViewRecommendH = (RecyclerView) view.findViewById(R.id.recyclerView_recommend_h);

        //创建recyclerView的布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        //设置布局管理器的方向为横向
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerViewRecommendH.setLayoutManager(linearLayoutManager);

        mRecyclerViewRecommendV = (RecyclerView) view.findViewById(R.id.recyclerView_recommend_v);
        mRecyclerViewRecommendV.setLayoutManager(new LinearLayoutManager(mContext));


        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void dataToView(RecommendBean recommendBean) {
        // 推荐页面的GridView(顶部)
        Recon_GridViewData(recommendBean);
        // 横向的RecyclerView
        Recon_RecyclerViewHData(recommendBean);
        // 纵向的RecyclerView
        Recon_RecyclerViewVData(recommendBean);

    }

    private void Recon_GridViewData(RecommendBean recommendBean) {
        List<RecommendBean.DataBean.IconListBean> icon_list =
                recommendBean.getData().getIcon_list();
        Recon_GridView_Adapter adapter =
                new Recon_GridView_Adapter(mContext, icon_list);
        mMyGridViewRecommend.setAdapter(adapter);
    }

    private void Recon_RecyclerViewHData(RecommendBean recommendBean) {
        List<RecommendBean.DataBean.TagListBean> tag_list =
                recommendBean.getData().getTag_list();
        Recon_RecycylerViewH_Adapter adapter =
                new Recon_RecycylerViewH_Adapter(mContext, tag_list);
        mRecyclerViewRecommendH.setAdapter(adapter);
    }

    private void Recon_RecyclerViewVData(RecommendBean recommendBean) {
        List<RecommendBean.DataBean.IndexListBean> index_list =
                recommendBean.getData().getIndex_list();

        Recon_RecycylerViewV_Adapter adapter = new
                Recon_RecycylerViewV_Adapter(mContext, index_list);
        mRecyclerViewRecommendV.setAdapter(adapter);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
